using System;
using HCore.Core;
using HCore.IOC.Beans.Factory.Attribute.Attr;
using HCore.Unity.Behaviour;
using HCore.Unity.Core;
using HCore.Unity.Shader;
using Session;
using UnityEngine;

public class GameManager : HCoreMonoSingletonBehaviour<GameManager>
{
    [SerializeField] public bool Debug;
    [SerializeField] public int TileSize = 170;
    [SerializeField] public int TileCount = 360;
    [SerializeField] public string JigsawImagePath = "https://s.panlai.com/upload/2023/11/bizhihui_com_20231109175806169952388671766.jpg";
    [SerializeField] public bool RotateTile = true;

    [Autowired] private GameDataService aGameDataService;
    [Autowired] private SessionService aSessionService;

    protected override void Awake()
    {
        base.Awake();
        mRouter.Register((newValue, oldValue) => PublishEvent(new RouterEvent(newValue, oldValue)));
        aSessionService.AddNewGameStartBeforeEvent(_ => DispatchRouter(Router.GamePage));
    }

    private void Start()
    {
        // 立即触发一次。把初始化推送出去。
        mRouter.Trigger();
        // 设置路由到“开始界面”
        DispatchRouter(Router.StartPage);
    }

    /// <summary>
    ///     开始一场新的游戏
    /// </summary>
    public void StartGame()
    {
        aGameDataService.AddGameDataLoadCompleteEvent(() => aSessionService.StartNewGame(JigsawImagePath, TileSize, TileCount, RotateTile, Debug));
    }


    #region 主相机移动

    /// <summary>
    ///     开启游戏面板移动（开启主相机移动）
    /// </summary>
    public static void EnableCameraMove()
    {
        GameUtils.MainCameraController.enabled = true;
    }

    /// <summary>
    ///     关闭游戏面板移动（关闭主相机移动）
    /// </summary>
    public static void DisabledCameraMove()
    {
        GameUtils.MainCameraController.enabled = false;
    }

    #endregion

    #region 游戏界面模糊

    /// <summary>
    ///     启动游戏界面模糊
    /// </summary>
    public static void EnableGameBoardBlur()
    {
        GaussianBlurManager.Instance.EnableBlurRender(GameUtils.UICamera);
    }

    /// <summary>
    ///     关闭游戏界面模糊
    /// </summary>
    public static void DisabledGameBoardBlur()
    {
        GaussianBlurManager.Instance.DisabledBlurRender(GameUtils.UICamera);
    }

    #endregion

    #region 路由

    /// <summary>
    ///     页面路由，通过路由变更进行 UI 页面的渲染和触发页面的显示隐藏事件
    /// </summary>
    private readonly BindableProperty<Router> mRouter = new();

    /// <summary>
    ///     <ul>
    ///         调度路由
    ///         <li>每次路由变更，会触发对应的“进出事件”。</li>
    ///         <li>路由逻辑必须准寻如下规范：进入时可以处理全局逻辑和自身逻辑；退出时，只能处理自身逻辑。例如相机移动是全局逻辑必须在进入时处理。页面开关是自身逻辑，必须进出都要处理。</li>
    ///     </ul>
    /// </summary>
    /// <param name="router"></param>
    public static void DispatchRouter(Router router)
    {
        Instance.mRouter.Value = router;
    }

    /// <summary>
    ///     获取路由
    /// </summary>
    /// <returns></returns>
    public static Router GetRouter()
    {
        return Instance.mRouter.Value;
    }

    #endregion
}

public class RouterEvent
{
    public RouterEvent(Router newRouter, Router oldRouter)
    {
        OldRouter = oldRouter;
        NewRouter = newRouter;
    }

    public Router OldRouter { get; }
    public Router NewRouter { get; }
}

public enum Router
{
    None, // 初始状态
    StartPage, // 开始页面
    GamePage, // 游戏界面
    GamePageMenu // 游戏界面菜单
}